package ils.services;

import ils.domain.catalog.model.Book;
import ils.domain.catalog.model.BookRepository;
import ils.domain.model.User;
import ils.domain.model.UserRepository;
import ils.domain.ugc.model.Review;
import ils.domain.ugc.model.ReviewRepository;
import ils.util.HibernateSession;
import ils.util.HibernateSessionManager;
import ils.web.model.WReview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

public class UgcServiceImpl implements UgcService
{
    private ReviewRepository reviewRepository;
    private BookRepository bookRepository;
    private UserRepository userRepository;
    private HibernateSessionManager hibernateSessionManager;

    @Override
    public List<WReview> getReviewsForBook(int bookId, int firstResult) throws Exception
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Review> reviews = reviewRepository.getReviewsForBook(bookId, firstResult, REVIEWS_BOOK_PAGE_SIZE);
            List<WReview> wReviews = new ArrayList<WReview>();
            if(reviews.size() == 0) return wReviews;
            List<Integer> bookIds = new ArrayList<Integer>();
            List<Integer> userIds = new ArrayList<Integer>();
            for(Review review : reviews)
            {
                WReview wReview = new WReview();
                BeanUtils.copyProperties(wReview, review);
                wReviews.add(wReview);
                bookIds.add(review.getBookId());
                userIds.add(review.getUserId());
            }
            Map<Integer, Book> books = bookRepository.getBooks(bookIds);
            Map<Integer, User> users = userRepository.getUsers(userIds);
            for(WReview wReview : wReviews)
            {
                Book book = books.get(wReview.getBookId());
                wReview.setBookName(book.getTitle());
                User user = users.get(wReview.getUserId());
                wReview.setUserName(user.getFirstName());
            }
            return wReviews;
        }
    }

    @Override
    public List<WReview> getReviewsOfUser(int userId, int firstResult)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void setReviewRepository(ReviewRepository reviewRepository)
    {
        this.reviewRepository = reviewRepository;
    }

    @Override
    public Review createReview(Review review)
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            return reviewRepository.createReview(review);
        }
    }

    public void setBookRepository(BookRepository bookRepository)
    {
        this.bookRepository = bookRepository;
    }

    public void setUserRepository(UserRepository userRepository)
    {
        this.userRepository = userRepository;
    }

    @Override
    public WReview getReviewUserBook(int userId, int bookId) throws Exception
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Review review = reviewRepository.getReviewUserBook(userId, bookId);
            if(review == null) return null;
            WReview wReview = new WReview();
            BeanUtils.copyProperties(wReview, review);
            Book book = bookRepository.getBook(bookId);
            User user = userRepository.getUser(userId);
            wReview.setUserName(user.getFirstName());
            wReview.setBookName(book.getTitle());
            return wReview;
        }
    }

    public void setHibernateSessionManager(HibernateSessionManager hibernateSessionManager) {
        this.hibernateSessionManager = hibernateSessionManager;
    }
}
